package simbot.example.game;

import cn.hutool.core.img.ImgUtil;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.*;

import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;

@Service
public class wuziqi {


    private static BufferedImage image = null;
    private static Graphics mainGraphics = null, graphics = null;
    private static Container panel = null;
    private static ByteArrayOutputStream outputStream = null;


    public int[][] qiPan = new int[15][15];
    private int[][] chessValue = new int[15][15];
    private int[][] chessValue2 = new int[15][15];

    private int maxi;
    private int maxj;


    public void setQiPan(int[][] qipan) {
        qiPan = qipan;
    }


    public boolean bai(int x, int y, File file) {
//        int[] ai = ai2(qiPan);
        qiPan[x][y] = 2;
        getImageBai(x, y, file);
        boolean win = isWin(x, y, 2);
        if (win) {
            System.out.println("白赢");
            return true;
        } else {
            return false;
        }
    }

    public boolean hei(File file) {
        int[] ai = ai(qiPan);
        qiPan[ai[0]][ai[1]] = 1;
        getImageHei(ai[0], ai[1], file);
        boolean win = isWin(ai[0], ai[1], 1);
        if (win) {
            System.out.println("黑赢");
            return true;
        } else {
            return false;
        }
    }


    public BufferedImage resizeImage(int x, int y, BufferedImage bfi) {
        BufferedImage bufferedImage = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);
        bufferedImage.getGraphics().drawImage(
                bfi.getScaledInstance(x, y, Image.SCALE_SMOOTH), 0, 0, null);
        return bufferedImage;
    }

    public void getImageHei(int x, int y, File file) {
        try {
            image = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }


        outputStream = new ByteArrayOutputStream();

        try {
            //设置图片大小
            BufferedImage background = resizeImage(535, 535, ImageIO.read(file));


            Graphics2D g = background.createGraphics();
            g.drawImage(ImageIO.read(new File("D:\\steam\\steamapps\\common\\wuzi\\黑.png")), 15 + x * 35, 15 + y * 35, 20, 20, null);
            g.dispose();
//            ImageIO.write(background, "jpg", new File("这里是一个输出图片的路径"));
            ImageIO.write(background, "JPG", outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }

        byte[] content = outputStream.toByteArray();//得到字节流中的所有字节
        try (BufferedOutputStream inputStream = new BufferedOutputStream(new FileOutputStream(file))) {
            //将所传过来的字节流中的字节设置为输出流
            inputStream.write(content);//将字节写出到指定位置
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getImageBai(int x, int y, File file) {
//        File file = new File("D:\\steam\\steamapps\\common\\wuzi\\2.jpg");
        try {
            image = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }


        outputStream = new ByteArrayOutputStream();

        try {
            //设置图片大小
            BufferedImage background = resizeImage(535, 535, ImageIO.read(file));


            Graphics2D g = background.createGraphics();
            g.drawImage(ImageIO.read(new File("D:\\steam\\steamapps\\common\\wuzi\\白.png")), 15 + x * 35, 15 + y * 35, 20, 20, null);
            g.dispose();
//            ImageIO.write(background, "jpg", new File("这里是一个输出图片的路径"));
            ImageIO.write(background, "JPG", outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }

        byte[] content = outputStream.toByteArray();//得到字节流中的所有字节
        try (BufferedOutputStream inputStream = new BufferedOutputStream(new FileOutputStream(file))) {
            //将所传过来的字节流中的字节设置为输出流
            inputStream.write(content);//将字节写出到指定位置
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static HashMap<String, Integer> getHm() {
        HashMap<String, Integer> hm = new HashMap();
        hm.put("1", 20);

        hm.put("11", 400);

        hm.put("111", 420);

        hm.put("1111", 30000);

        hm.put("12", 4);

        hm.put("112", 40);

        hm.put("1112", 400);

        hm.put("11112", 10000);

        hm.put("21111", 10000);


        hm.put("2", 8);

        hm.put("22", 80);

        hm.put("222", 1000);

        hm.put("2222", 5000);

        hm.put("22221", 5000);

        hm.put("222211", 5000);

        hm.put("21", 6);

        hm.put("221", 60);

        hm.put("2221", 600);

        hm.put("121", 5);

        hm.put("1221", 5);

        hm.put("2112", 5);

        hm.put("212", 5);
        return hm;
    }

    public int[] ai(int[][] chess) {
        HashMap<String, Integer> hm = getHm();
        for (int i = 0; i < chess.length; i++)

            for (int j = 0; j < chess.length; j++) {
                if (chess[i][j] == 0) {
// 向右

                    StringBuilder code = new StringBuilder();

                    int color = 1;

                    for (int k = i + 1; k < chess.length; k++) {
                        if (chess[k][j] == 0)

                            break;

                        else {// 有棋子

                            if (color == 1) {
                                color = chess[k][j];// 保存第一颗棋子的颜色

                                code.append(chess[k][j]);// 保存棋子相连的情况

                            } else if (chess[k][j] == color)

                                code.append(chess[k][j]);// 保存棋子相连的情况

                            else {// 不同棋子跳出循环

                                code.append(chess[k][j]);// 保存棋子相连的情况

                                break;

                            }

                        }

                    }

                    Integer value = hm.get(code.toString());

                    if (value != null)

                        chessValue[i][j] += value; // 权值累加

// 向左

                    code = new StringBuilder();

                    color = 1;

                    for (int k = i - 1; k >= 0; k--) {
                        if (chess[k][j] == 0)

                            break;

                        else {
                            if (color == 1) {
                                color = chess[k][j];

                                code.append(chess[k][j]);

                            } else if (chess[k][j] == color)

                                code.append(chess[k][j]);

                            else {
                                code.append(chess[k][j]);

                                break;

                            }

                        }

                        value = hm.get(code.toString());

                        if (value != null)

                            chessValue[i][j] += value; // 权值累加

                    }

// 向下

                    code = new StringBuilder();

                    color = 1;

                    for (int k = j + 1; k < chess.length; k++) {
                        if (chess[i][k] == 0)

                            break;

                        else {
                            if (color == 1) {
                                color = chess[i][k];

                                code.append(chess[i][k]);

                            } else if (chess[i][k] == color)

                                code.append(chess[i][k]);

                            else {
                                code.append(chess[i][k]);

                                break;

                            }

                        }

                        value = hm.get(code.toString());

                        if (value != null)

                            chessValue[i][j] += value; // 权值累加

                    }

// 向上

                    code = new StringBuilder();

                    color = 1;

                    for (int k = j - 1; k >= 0; k--) {
                        if (chess[i][k] == 0)

                            break;

                        else {
                            if (color == 1) {
                                color = chess[i][k];

                                code.append(chess[i][k]);

                            } else if (chess[i][k] == color)

                                code.append(chess[i][k]);

                            else {
                                code.append(chess[i][k]);

                                break;

                            }

                        }

                        value = hm.get(code.toString());

                        if (value != null)

                            chessValue[i][j] += value; // 权值累加

                    }

// 左上

                    code = new StringBuilder();

                    color = 1;

                    for (int m = i - 1, n = j - 1; m >= 0 && n >= 0; m--, n--) {
                        if (chess[m][n] == 0)

                            break;

                        else if (color == 1) {
                            color = chess[m][n];

                            code.append(chess[m][n]);

                        } else if (chess[m][n] == color)

                            code.append(chess[m][n]);

                        else {
                            code.append(chess[m][n]);

                            break;

                        }

                        value = hm.get(code.toString());

                        if (value != null)

                            chessValue[i][j] += value; // 权值累加

                    }

// 右上

                    code = new StringBuilder();

                    color = 1;

                    for (int m = i + 1, n = j - 1; m < chess.length && n >= 0; m++, n--) {
                        if (chess[m][n] == 0)

                            break;

                        else {
                            if (color == 1) {
                                color = chess[m][n];

                                code.append(chess[m][n]);

                            } else if (chess[m][n] == color)

                                code.append(chess[m][n]);

                            else {
                                code.append(chess[m][n]);

                                break;

                            }

                        }

                        value = hm.get(code.toString());

                        if (value != null)

                            chessValue[i][j] += value; // 权值累加

                    }

// 左下

                    code = new StringBuilder();

                    color = 1;

                    for (int m = i - 1, n = j + 1; m >= 0 && n < chess.length; m--, n++) {
                        if (chess[m][n] == 0)

                            break;

                        else {
                            if (color == 1) {
                                color = chess[m][n];

                                code.append(chess[m][n]);

                            } else if (chess[m][n] == color)

                                code.append(chess[m][n]);

                            else {
                                code.append(chess[m][n]);

                                break;

                            }

                        }

                        value = hm.get(code.toString());

                        if (value != null)

                            chessValue[i][j] += value; // 权值累加

                    }

// 右下

                    code = new StringBuilder();

                    color = 1;

                    for (int m = i + 1, n = j + 1; m < chess.length && n < chess.length; m++, n++) {
                        if (chess[m][n] == 0)

                            break;

                        else {
                            if (color == 1) {
                                color = chess[m][n];

                                code.append(chess[m][n]);

                            } else if (chess[m][n] == color)

                                code.append(chess[m][n]);

                            else {
                                code.append(chess[m][n]);

                                break;

                            }

                        }

                        value = hm.get(code.toString());

                        if (value != null)

                            chessValue[i][j] += value; // 权值累加

                    }

                }

            }

        int maxv = 0;

        for (int j = 0; j < chess.length; j++)

            for (int i = 0; i < chess.length; i++) {
                if (maxv < chessValue[i][j]) {
                    maxv = chessValue[i][j];

                    maxi = i;

                    maxj = j;

                }

            }

        for (int j = 0; j < chess.length; j++)

            for (int i = 0; i < chess.length; i++)

                chessValue[i][j] = 0;

        return new int[]{maxi, maxj};
    }


    public int[] ai2(int[][] chess) {
        HashMap<String, Integer> hm = getHm();
        for (int i = 0; i < chess.length; i++)

            for (int j = 0; j < chess.length; j++) {
                if (chess[i][j] == 0) {
// 向右

                    StringBuilder code = new StringBuilder();

                    int color = 2;

                    for (int k = i + 1; k < chess.length; k++) {
                        if (chess[k][j] == 0)

                            break;

                        else {// 有棋子

                            if (color == 2) {
                                color = chess[k][j];// 保存第一颗棋子的颜色

                                code.append(chess[k][j]);// 保存棋子相连的情况

                            } else if (chess[k][j] == color)

                                code.append(chess[k][j]);// 保存棋子相连的情况

                            else {// 不同棋子跳出循环

                                code.append(chess[k][j]);// 保存棋子相连的情况

                                break;

                            }

                        }

                    }

                    Integer value = hm.get(code.toString());

                    if (value != null)

                        chessValue2[i][j] += value; // 权值累加

// 向左

                    code = new StringBuilder();

                    color = 2;

                    for (int k = i - 1; k >= 0; k--) {
                        if (chess[k][j] == 0)

                            break;

                        else {
                            if (color == 2) {
                                color = chess[k][j];

                                code.append(chess[k][j]);

                            } else if (chess[k][j] == color)

                                code.append(chess[k][j]);

                            else {
                                code.append(chess[k][j]);

                                break;

                            }

                        }

                        value = hm.get(code.toString());

                        if (value != null)

                            chessValue2[i][j] += value; // 权值累加

                    }

// 向下

                    code = new StringBuilder();

                    color = 2;

                    for (int k = j + 1; k < chess.length; k++) {
                        if (chess[i][k] == 0)

                            break;

                        else {
                            if (color == 2) {
                                color = chess[i][k];

                                code.append(chess[i][k]);

                            } else if (chess[i][k] == color)

                                code.append(chess[i][k]);

                            else {
                                code.append(chess[i][k]);

                                break;

                            }

                        }

                        value = hm.get(code.toString());

                        if (value != null)

                            chessValue2[i][j] += value; // 权值累加

                    }

// 向上

                    code = new StringBuilder();

                    color = 2;

                    for (int k = j - 1; k >= 0; k--) {
                        if (chess[i][k] == 0)

                            break;

                        else {
                            if (color == 2) {
                                color = chess[i][k];

                                code.append(chess[i][k]);

                            } else if (chess[i][k] == color)

                                code.append(chess[i][k]);

                            else {
                                code.append(chess[i][k]);

                                break;

                            }

                        }

                        value = hm.get(code.toString());

                        if (value != null)

                            chessValue2[i][j] += value; // 权值累加

                    }

// 左上

                    code = new StringBuilder();

                    color = 2;

                    for (int m = i - 1, n = j - 1; m >= 0 && n >= 0; m--, n--) {
                        if (chess[m][n] == 0)

                            break;

                        else if (color == 2) {
                            color = chess[m][n];

                            code.append(chess[m][n]);

                        } else if (chess[m][n] == color)

                            code.append(chess[m][n]);

                        else {
                            code.append(chess[m][n]);

                            break;

                        }

                        value = hm.get(code.toString());

                        if (value != null)

                            chessValue2[i][j] += value; // 权值累加

                    }

// 右上

                    code = new StringBuilder();

                    color = 2;

                    for (int m = i + 1, n = j - 1; m < chess.length && n >= 0; m++, n--) {
                        if (chess[m][n] == 0)

                            break;

                        else {
                            if (color == 2) {
                                color = chess[m][n];

                                code.append(chess[m][n]);

                            } else if (chess[m][n] == color)

                                code.append(chess[m][n]);

                            else {
                                code.append(chess[m][n]);

                                break;

                            }

                        }

                        value = hm.get(code.toString());

                        if (value != null)

                            chessValue2[i][j] += value; // 权值累加

                    }

// 左下

                    code = new StringBuilder();

                    color = 2;

                    for (int m = i - 1, n = j + 1; m >= 0 && n < chess.length; m--, n++) {
                        if (chess[m][n] == 0)

                            break;

                        else {
                            if (color == 2) {
                                color = chess[m][n];

                                code.append(chess[m][n]);

                            } else if (chess[m][n] == color)

                                code.append(chess[m][n]);

                            else {
                                code.append(chess[m][n]);

                                break;

                            }

                        }

                        value = hm.get(code.toString());

                        if (value != null)

                            chessValue2[i][j] += value; // 权值累加

                    }

// 右下

                    code = new StringBuilder();

                    color = 2;

                    for (int m = i + 1, n = j + 1; m < chess.length && n < chess.length; m++, n++) {
                        if (chess[m][n] == 0)

                            break;

                        else {
                            if (color == 2) {
                                color = chess[m][n];

                                code.append(chess[m][n]);

                            } else if (chess[m][n] == color)

                                code.append(chess[m][n]);

                            else {
                                code.append(chess[m][n]);

                                break;

                            }

                        }

                        value = hm.get(code.toString());

                        if (value != null)

                            chessValue2[i][j] += value; // 权值累加

                    }

                }

            }

        int maxv = 0;

        for (int j = 0; j < chess.length; j++)

            for (int i = 0; i < chess.length; i++) {
                if (maxv < chessValue2[i][j]) {
                    maxv = chessValue2[i][j];

                    maxi = i;

                    maxj = j;

                }

            }

        for (int j = 0; j < chess.length; j++)

            for (int i = 0; i < chess.length; i++)

                chessValue2[i][j] = 0;

        return new int[]{maxi, maxj};
    }


    public boolean isWin(int x, int y, int color) {
        int cont = 0;
        int zx = isEq(x, y, "up", cont, color) + isEq(x, y, "down", cont, color);
        int hx = isEq(x, y, "left", cont, color) + isEq(x, y, "right", cont, color);
        int zsyx = isEq(x, y, "lu", cont, color) + isEq(x, y, "rd", cont, color);
        int zxys = isEq(x, y, "ru", cont, color) + isEq(x, y, "ld", cont, color);
        if (zx >= 6) {
            return true;
        }
        if (hx >= 6) {
            return true;
        }
        if (zsyx >= 6) {
            return true;
        }
        if (zxys >= 6) {
            return true;
        }

        return false;
    }

    public int isEq(int x, int y, String dir, int cont, int color) {
        int a = cont;
        if (qiPan[x][y] == color) {
            if (dir.equals("up") && y > 0) {
                a = a + 1;
                return isEq(x, y - 1, dir, a, color);
            }
            if (dir.equals("down") && y < 14) {
                a = a + 1;
                return isEq(x, y + 1, dir, a, color);
            }
            if (dir.equals("left") && x > 0) {
                a = a + 1;
                return isEq(x - 1, y, dir, a, color);
            }
            if (dir.equals("right") && x < 14) {
                a = a + 1;
                return isEq(x + 1, y, dir, a, color);
            }
            if (dir.equals("lu") && x > 0 && y > 0) {
                a = a + 1;
                return isEq(x - 1, y - 1, dir, a, color);
            }
            if (dir.equals("ru") && x < 14 && y > 0) {
                a = a + 1;
                return isEq(x + 1, y - 1, dir, a, color);
            }
            if (dir.equals("ld") && x > 0 && y < 14) {
                a = a + 1;
                return isEq(x - 1, y + 1, dir, a, color);
            }
            if (dir.equals("rd") && x < 14 && y < 14) {
                a = a + 1;
                return isEq(x + 1, y, dir + 1, a, color);
            }
        }
        return a;
    }

//    public String isEq2(int x, int y, String dir, String code, int cont) {
//        String a = "";
//        int b = cont;
//        if (dir.equals("up") && y > 0) {
//            a = code + qiPan[x][y];
//            b++;
//            return isEq2(x, y - 1, dir, a, b);
//        }
//        if (dir.equals("down") && y < 14) {
//            a = code + qiPan[x][y];
//            b++;
//            return isEq2(x, y + 1, dir, a, b);
//        }
//        if (dir.equals("left") && x > 0) {
//            a = code + qiPan[x][y];
//            b++;
//            return isEq2(x - 1, y, dir, a, b);
//        }
//        if (dir.equals("right") && x < 14) {
//            a = code + qiPan[x][y];
//            b++;
//            return isEq2(x + 1, y, dir, a, b);
//        }
//        if (dir.equals("lu") && x > 0 && y > 0) {
//            a = code + qiPan[x][y];
//            b++;
//            return isEq2(x - 1, y - 1, dir, a, b);
//        }
//        if (dir.equals("ru") && x < 14 && y > 0) {
//            a = code + qiPan[x][y];
//            b++;
//            return isEq2(x + 1, y - 1, dir, a, b);
//        }
//        if (dir.equals("ld") && x > 0 && y < 14) {
//            a = code + qiPan[x][y];
//            b++;
//            return isEq2(x - 1, y + 1, dir, a, b);
//        }
//        if (dir.equals("rd") && x < 14 && y < 14) {
//            a = code + qiPan[x][y];
//            b++;
//            return isEq2(x + 1, y, dir + 1, a, b);
//        }
//        return a;
//    }
//
//
//    public void newAi() {
//        String code = "";
//        for (int x = 0; x < 15; x++) {
//            for (int y = 0; y < 15; y++) {
//                for (int i = 0; i < 5; i++) {
//
//                }
//            }
//        }
//    }

}